export const format = (shijianchuo: string | number, type = 'yyyy-MM-dd hh:mm:ss') => {
    if (!shijianchuo) return '/'
    //shijianchuo是整数，否则要parseInt转换
    const time = new Date(shijianchuo)
    const y = time.getFullYear()
    const m = time.getMonth() + 1
    const d = time.getDate()
    const h = time.getHours()
    const mm = time.getMinutes()
    const s = time.getSeconds()
    const add0 = (m: number): string => {
        return m < 10 ? '0' + m : m + ''
    }
    switch (type) {
        case 'minDay':
            return y + '/' + add0(m) + '/' + '01'
        default:
            return type
                .replace('yyyy', y + '')
                .replace('yy', y.toString().slice(-2))
                .replace('MM', add0(m))
                .replace('dd', add0(d))
                .replace('hh', add0(h))
                .replace('mm', add0(mm))
                .replace('ss', add0(s))
    }
}

export const utilCheckPermission = (value: string[]): boolean => {
    const userInfo = getStorageUserInfo()
    if (value && value.length > 0) {
        const permissions = userInfo.permissions || []
        const hasPermission = permissions.some((permission: string) => {
            return value.includes(permission)
        })

        return hasPermission
    } else {
        return false
    }
}
export const utilLogout = () => {
    localStorage.setItem("siteUserInfo", JSON.stringify({}));
    window.location.href = "/xls/#/login"
}
export const getStorageUserInfo = () => {
    const userInfo = JSON.parse(localStorage.getItem('siteUserInfo') || "{}")
    return userInfo
}
export const downLoadFile = (blob: Blob, name: string) => {
    const url = URL.createObjectURL(blob);
    // 创建一个a标签用于触发下载
    const downloadLink = document.createElement('a');
    downloadLink.href = url;
    downloadLink.download = name; // 指定下载文件的名称

    // 添加链接到文档并模拟点击
    document.body.appendChild(downloadLink);
    downloadLink.click();

    // 清理并移除元素和URL引用
    document.body.removeChild(downloadLink);
    URL.revokeObjectURL(url);
}

